home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1997 August / Walnut Creek CDROM.7z / VOL_400 / 402_01 / CFORMS_2 / PIC_LIB / PIC_DIRE.FRM < prev   
Encoding:
Text File  |  1993-07-20  |  7.3 KB  |  302 lines

  1. /*
  2.  * Library picture: let user select a file.
  3.  *
  4.  * @(#) pic_dirent.frm,v 1.6 1993/07/20 23:05:55 lasse Exp
  5.  */
  6.  
  7. Viewport view {
  8.     Pos 0,0;
  9.     Size 80, 23;
  10. }
  11.  
  12. Event Key ESC   { pic_leave(); }
  13. Event Key DOWN  { fld_move(fld_down(NULL));  }
  14. Event Key UP    { fld_move(fld_up(NULL));    }
  15. Event Key TAB   { fld_move(fld_next(NULL)); }
  16. Event Key CR    {
  17.     char *p = fld_get_trimmed(NULL);
  18.  
  19.     if (strcmp(p, "../") == 0)
  20.     {
  21.     p = strrchr(dirname, '/');
  22.     if (p != NULL)
  23.     {
  24.         /* Remove concatenated slashes */
  25.         while ((p > dirname) && (p[-1] == '/')) {
  26.         p--;
  27.         }
  28.  
  29.         if (p == dirname) {
  30.         p++; /* Never remove root slash */
  31.         }
  32.         *p = 0;
  33.     }
  34.     }
  35.     else if (strcmp(p, "./") != 0)
  36.     {
  37.     strcat(dirname, "/");
  38.     strcat(dirname, p);
  39.  
  40.     if (dirname[strlen(dirname) - 1] == '/') {
  41.         dirname[strlen(dirname) - 1] = 0;
  42.     }
  43.  
  44.     if (!isadir(dirname, NULL))
  45.     {
  46.         fld_set(field("Return"), dirname);
  47.         pic_leave();
  48.         return;
  49.     }
  50.     }
  51.  
  52.     fld_set(field("Dir"), dirname);
  53.     redraw_dir();
  54.     fld_move(field("Row0"));
  55. }
  56.  
  57. Picture pic_dirent Viewport View
  58. {
  59.     Event Draw {
  60.     extern char *getcwd(char *buf, int size);
  61.     if (fld_isempty(field("Dir"))) {
  62.         getcwd(dirname, sizeof dirname);
  63.         fld_set(field("Dir"), dirname);
  64.     }
  65.     redraw_dir();
  66.     fld_move(field("Row0"));
  67.     }
  68.  
  69.     Field Title {
  70.     Pos Center, 1;
  71.     Type Char(30);
  72.     Forbidden;
  73.     Value "Choose file";
  74.     }
  75.  
  76.     Field Dir {
  77.     Event Key CR { fld_move(fld_down(NULL)); }
  78.     Event Exit {
  79.         if (fld_ismodified(NULL)) {
  80.         redraw_dir();
  81.         }
  82.     }
  83.     Pos 12, +2; Type Char(60);
  84.     }
  85.  
  86.     Field DirLabel {
  87.     Type Char(12);
  88.     Pos 0, +0;
  89.     Forbidden;
  90.     Value " Directory: ";
  91.     }
  92.  
  93.     Field Row0  {Pos 0,    5; Type Char(19); Protected; }
  94.     Field Row1  {Pos +20, +0; Type Char(19); Protected; }
  95.     Field Row2  {Pos +20, +0; Type Char(19); Protected; }
  96.     Field Row3  {Pos +20, +0; Type Char(19); Protected; }
  97.  
  98.     Field Row4  {Pos 0,   +1; Type Char(19); Protected; }
  99.     Field Row5  {Pos +20, +0; Type Char(19); Protected; }
  100.     Field Row6  {Pos +20, +0; Type Char(19); Protected; }
  101.     Field Row7  {Pos +20, +0; Type Char(19); Protected; }
  102.  
  103.     Field Row8  {Pos 0,   +1; Type Char(19); Protected; }
  104.     Field Row9  {Pos +20, +0; Type Char(19); Protected; }
  105.     Field Row10  {Pos +20, +0; Type Char(19); Protected; }
  106.     Field Row11  {Pos +20, +0; Type Char(19); Protected; }
  107.  
  108.     Field Row12  {Pos 0,   +1; Type Char(19); Protected; }
  109.     Field Row13  {Pos +20, +0; Type Char(19); Protected; }
  110.     Field Row14  {Pos +20, +0; Type Char(19); Protected; }
  111.     Field Row15  {Pos +20, +0; Type Char(19); Protected; }
  112.  
  113.     Field Row16  {Pos 0,   +1; Type Char(19); Protected; }
  114.     Field Row17  {Pos +20, +0; Type Char(19); Protected; }
  115.     Field Row18  {Pos +20, +0; Type Char(19); Protected; }
  116.     Field Row19  {Pos +20, +0; Type Char(19); Protected; }
  117.  
  118.     Field Row20  {Pos 0,   +1; Type Char(19); Protected; }
  119.     Field Row21  {Pos +20, +0; Type Char(19); Protected; }
  120.     Field Row22  {Pos +20, +0; Type Char(19); Protected; }
  121.     Field Row23  {Pos +20, +0; Type Char(19); Protected; }
  122.  
  123.     Field Row24  {Pos 0,   +1; Type Char(19); Protected; }
  124.     Field Row25  {Pos +20, +0; Type Char(19); Protected; }
  125.     Field Row26  {Pos +20, +0; Type Char(19); Protected; }
  126.     Field Row27  {Pos +20, +0; Type Char(19); Protected; }
  127.  
  128.     Field Row28  {Pos 0,   +1; Type Char(19); Protected; }
  129.     Field Row29  {Pos +20, +0; Type Char(19); Protected; }
  130.     Field Row30  {Pos +20, +0; Type Char(19); Protected; }
  131.     Field Row31  {Pos +20, +0; Type Char(19); Protected; }
  132.  
  133.     Field Row32  {Pos 0,   +1; Type Char(19); Protected; }
  134.     Field Row33  {Pos +20, +0; Type Char(19); Protected; }
  135.     Field Row34  {Pos +20, +0; Type Char(19); Protected; }
  136.     Field Row35  {Pos +20, +0; Type Char(19); Protected; }
  137.  
  138.     Field Row36  {Pos 0,   +1; Type Char(19); Protected; }
  139.     Field Row37  {Pos +20, +0; Type Char(19); Protected; }
  140.     Field Row38  {Pos +20, +0; Type Char(19); Protected; }
  141.     Field Row39  {Pos +20, +0; Type Char(19); Protected; }
  142.  
  143.     Field Row40  {Pos 0,   +1; Type Char(19); Protected; }
  144.     Field Row41  {Pos +20, +0; Type Char(19); Protected; }
  145.     Field Row42  {Pos +20, +0; Type Char(19); Protected; }
  146.     Field Row43  {Pos +20, +0; Type Char(19); Protected; }
  147.  
  148.     Field Row44  {Pos 0,   +1; Type Char(19); Protected; }
  149.     Field Row45  {Pos +20, +0; Type Char(19); Protected; }
  150.     Field Row46  {Pos +20, +0; Type Char(19); Protected; }
  151.     Field Row47  {Pos +20, +0; Type Char(19); Protected; }
  152.  
  153.     Field Row48  {Pos 0,   +1; Type Char(19); Protected; }
  154.     Field Row49  {Pos +20, +0; Type Char(19); Protected; }
  155.     Field Row50  {Pos +20, +0; Type Char(19); Protected; }
  156.     Field Row51  {Pos +20, +0; Type Char(19); Protected; }
  157.  
  158.     Field Row52  {Pos 0,   +1; Type Char(19); Protected; }
  159.     Field Row53  {Pos +20, +0; Type Char(19); Protected; }
  160.     Field Row54  {Pos +20, +0; Type Char(19); Protected; }
  161.     Field Row55  {Pos +20, +0; Type Char(19); Protected; }
  162.  
  163.     Field Help  {Pos Center, +2; Type Char(20); Forbidden; }
  164.     Field Return {Pos 0, +1; Type Char(80); Forbidden; Invisible; }
  165. }
  166.  
  167. CCode {
  168. #include <sys/types.h>
  169. #include <sys/stat.h>
  170. #include <dirent.h>
  171. #include <string.h>
  172. #include <stdlib.h>
  173. #include "../src/config.h"
  174.  
  175. #define MAXROWS 56 
  176.  
  177.     static int n_entries = 0;
  178.     static int cur_entry = 0;
  179.     static char dirname[1024];
  180.  
  181.     static int read_dir(char *dir);
  182.     static void showdir(void);
  183.     static int isadir(char *dir, char *fname);
  184.  
  185.     /*
  186.      * List of entries.
  187.      */
  188.     static struct direlem {
  189.     struct direlem *next;
  190.     char *name;
  191.     } *first = NULL;
  192.  
  193.     static int read_dir(char *dirname)
  194.     {
  195.     struct direlem *p, *next, **pp;
  196.     struct dirent *dent;
  197.     DIR *d;
  198.     
  199.     if ((d = opendir(dirname)) == NULL) return -1;
  200.  
  201.     /*
  202.      * Free old list.
  203.      */
  204.     for (p = first; p != NULL; p = next)
  205.     {
  206.         next = p->next;
  207.         if (p->name) free(p->name);
  208.         free(p);
  209.     }
  210.     first = NULL;
  211.  
  212.     /*
  213.      * Read the whole directory into list.
  214.      */
  215.     while(dent = readdir(d))
  216.     {
  217.         /*
  218.          * Allocate space for entry.
  219.          */
  220.         if ((p = malloc(sizeof *p)) == NULL) return -1;
  221.         /* Add 2 for tackle on trailing slash */
  222.         if ((p->name = malloc(dent->d_reclen + 2)) == NULL) return -1;
  223.  
  224.         /*
  225.          * Setup entry.
  226.          */
  227.         memcpy(p->name, dent->d_name, dent->d_reclen);
  228.         p->name[dent->d_reclen] = 0;
  229.  
  230.         if (isadir(dirname, p->name)) {
  231.         strcat(p->name, "/");
  232.         }
  233.  
  234.         /*
  235.          * Link entry.
  236.          */
  237.         for(pp = &first; *pp != NULL; pp = &(*pp)->next)
  238.         {
  239.         if (strcmp((*pp)->name, p->name) > 0) break;
  240.         }
  241.         p->next = *pp;
  242.         *pp = p;
  243.  
  244.         n_entries++;
  245.     }
  246.     return 0;
  247.     }
  248.  
  249.     static void showdir(void)
  250.     {
  251.     int i;
  252.     struct direlem *p;
  253.  
  254.     fld_set(field("Dir"), dirname);
  255.  
  256.     p = first;
  257.     for(i = 0; (i < cur_entry) && (p != NULL); i++) {
  258.         p = p->next;
  259.     }
  260.  
  261.     for(i = 0; (i < MAXROWS) && (p != NULL); i++)
  262.     {
  263.         fld_set(field("Row%d", i), p->name);
  264.         p = p->next;
  265.     }
  266.  
  267.     for(; i < MAXROWS; i++)
  268.     {
  269.         fld_set(field("Row%d", i), "");
  270.     }
  271.     }
  272.  
  273.     static void redraw_dir(void)
  274.     {
  275.     strcpy(dirname, fld_get_trimmed(field("Dir")));
  276.  
  277.     if (read_dir(dirname) != 0) {
  278.         message("Can't open directory: %s", dirname);
  279.         return;
  280.     }
  281.  
  282.     showdir();
  283.     }
  284.  
  285.     static int isadir(char *dir, char *fname)
  286.     {
  287.     static char buf[1024];
  288.     struct stat stat_buf;
  289.  
  290.     strcpy(buf, dir);
  291.     if (fname) {
  292.         strcat(buf, "/");
  293.         strcat(buf, fname);
  294.     }
  295.  
  296.     stat_buf.st_mode = 0;
  297.     stat(buf, &stat_buf);
  298.  
  299.     return (stat_buf.st_mode & S_IFDIR) != 0;
  300.     }
  301. }
  302.